﻿using System.Globalization;
using System.Text;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;

namespace XiaoSe.Web
{
    /// <summary>
    /// 公共方法
    /// </summary>
    public class Common
    {
        /// <summary>
        /// 获取查询字符串
        /// </summary>
        /// <param name="key">key</param>
        /// <returns>字符串</returns>
        public static string GetQueryStringToString(string key)
        {
            return HttpContext.Current.Request.QueryString[key] ?? "";
        }

        /// <summary>
        /// 获取查询字符串
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="defaultValue"></param>
        /// <returns>整数</returns>
        public static int GetQueryStringToInt(string key, int defaultValue)
        {
            int value;
            if (!int.TryParse(GetQueryStringToString(key), out value))
            {
                value = defaultValue;
            }
            return value;
        }
        /// <summary>
        /// 转换成HTML code
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>string</returns>
        public static string Encode(string str)
        {
            str = str.Replace("&", "&amp;");
            str = str.Replace("'", "''");
            str = str.Replace("\"", "&quot;");
            str = str.Replace(" ", "&nbsp;");
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            str = str.Replace("\n", "<br>");
            return str;
        }
        /// <summary>
        /// 解析html成普通文本
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>string</returns>
        public static string Decode(string str)
        {
            str = str.Replace("<br />", "\n");
            str = str.Replace("&gt;", ">");
            str = str.Replace("&lt;", "<");
            str = str.Replace("&nbsp;", " ");
            str = str.Replace("&quot;", "\"");
            return str;
        }
        /// <summary>
        /// 生成验证码byte[]
        /// </summary>
        /// <param name="iWidth"></param>
        /// <param name="iHeight"></param>
        /// <param name="iLeng"></param>
        /// <param name="sSessionName"></param>
        /// <returns></returns>
        public static byte[] CreateCheckCode(int iWidth, int iHeight, int iLeng, string sSessionName)
        {
            var bCode = new Bitmap(iWidth, iHeight);
            var gCode = Graphics.FromImage(bCode);
            var sCode = StringEx.CreateRandomStr(iLeng);
            HttpContext.Current.Session[sSessionName] = sCode;
            var sb = new SolidBrush(Color.Black);
            var drawFont = new Font("宋体", 12);
            float fStringStart = 0;
            const float fStringSpace = 0;
            foreach (char c in sCode)
            {
                gCode.DrawString(c.ToString(CultureInfo.InvariantCulture), drawFont, sb, new PointF(fStringStart, 0));
                fStringStart += gCode.MeasureString(c.ToString(CultureInfo.InvariantCulture), drawFont).Width + fStringSpace;
            }
            var p = new Pen(Color.Black, 1);
            gCode.DrawLine(p, 0, iHeight / 2, iWidth, iHeight / 2);
            var ms = new System.IO.MemoryStream();
            bCode.Save(ms, ImageFormat.Png);
            gCode.Dispose();
            bCode.Dispose();
            sb.Dispose();
            drawFont.Dispose();
            ms.Close();
            return ms.ToArray();
        }
        /// <summary>
        /// 生成分页《a》列表
        /// </summary>
        /// <param name="pageCount"></param>
        /// <param name="currentPage"></param>
        /// <param name="displayCount"></param>
        /// <param name="hrefFormat"></param>
        /// <param name="displayTextFormat"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="currentHtmlAttributes"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string CreatePagingAList(int pageCount, int currentPage, int displayCount, string hrefFormat, string displayTextFormat, string htmlAttributes, string currentHtmlAttributes, string separator)
        {
            int start;
            int end;
            if (pageCount <= 0)
            {
                return "";
            }
            if (pageCount <= displayCount)
            {
                start = 1;
                end = pageCount;
            }
            else
            {
                var leftCount = displayCount / 2;
                var rightCount = displayCount - leftCount;
                if ((currentPage - leftCount + 1) <= 1)
                {
                    start = 1;
                    end = displayCount;
                }
                else
                {
                    if ((currentPage + rightCount) >= pageCount)
                    {
                        start = pageCount - displayCount + 1;
                        end = pageCount;
                    }
                    else
                    {
                        start = (currentPage - leftCount + 1);
                        end = currentPage + rightCount;
                    }
                }
            }
            StringBuilder sbPagingAList = new StringBuilder();
            for (var i = start; i <= end; i++)
            {
                if (i != currentPage)
                {
                    sbPagingAList.Append("<a " + htmlAttributes + " " + string.Format(hrefFormat, i) + ">" + string.Format(displayTextFormat, i) + "</a>" + separator);
                }
                else
                {
                    sbPagingAList.Append("<a " + currentHtmlAttributes + " " + string.Format(hrefFormat, i) + ">" + string.Format(displayTextFormat, i) + "</a>" + separator);
                }
            }
            return sbPagingAList.ToString();
        }
    }
}
